package cn.wolfcode.trip.admin.controller;

import cn.wolfcode.trip.base.domain.Travel;
import cn.wolfcode.trip.base.domain.TravelContent;
import cn.wolfcode.trip.base.query.TravelQueryObject;
import cn.wolfcode.trip.base.service.ITravelService;
import cn.wolfcode.trip.base.util.JSONResult;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

@Controller
@RequestMapping("travel")
public class TravelController {
    //关联service
    @Autowired
    private ITravelService travelService;

    //待审核页面
    @RequestMapping("list")
    public String list(@ModelAttribute("qo")TravelQueryObject qo,Model model){
        //首页的效果是呈现出所有的待发布
       if(qo.getState()==null){
            qo.setIsPublic(true);
            qo.setState(Travel.STATE_WAIT);
        }
        //设置排序
        qo.setOrderby("t.lastUpdateTime asc");
        PageInfo<Travel> pageInfo = travelService.queryForList(qo);
        model.addAttribute("pageInfo",pageInfo);
        return "travel/list";
   }
   //已发布页面
    @RequestMapping("releaseList")
    public String release(@ModelAttribute("qo")TravelQueryObject qo,Model model){
        //首页的效果是呈现出所有的待发布
            qo.setIsPublic(true);
            qo.setState(Travel.STATE_RELEASE);
        //设置排序
        qo.setOrderby("t.releaseTime asc");
        PageInfo<Travel> pageInfo = travelService.queryForList(qo);
        model.addAttribute("pageInfo",pageInfo);
        return "travel/releaseList";
   }

   //根据id查出文章内容
    @RequestMapping("getContentById")
    @ResponseBody
    public TravelContent getContentById(Long id){
        return travelService.getContentById(id);
   }

   //根据id修改文章状态
    @RequestMapping("changeState")
    @ResponseBody
    public JSONResult changeState(Travel travel){
        travelService.changeState(travel);
        return new JSONResult();
   }

}
